package net.kodra.supereasy.traffic.client;

import java.util.HashMap;
import java.util.Map;

import net.kodra.supereasy.traffic.shared.TransportationVehicle;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.Maps;
import com.google.gwt.maps.client.control.LargeMapControl;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.geom.Point;
import com.google.gwt.maps.client.geom.Size;
import com.google.gwt.maps.client.overlay.Icon;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.maps.client.overlay.MarkerOptions;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;

public class DemoApp implements EntryPoint
{

  /**
   * Create a remote service proxy to talk to the server-side Traffic service.
  **/
  private final TrafficServiceAsync trafficService = GWT.create(TrafficService.class);

  @Override
  public void onModuleLoad()
  {
    Maps.loadMapsApi("", "2", false, new Runnable()
    {
      public void run()
      {
        buildUi();
      }
    });

    trafficService.registerClient(new RegistrationAsyncCallback());
  }

  private class RegistrationAsyncCallback implements AsyncCallback<Long>
  {

    @Override
    public void onFailure(Throwable caught)
    {
      Window.alert(caught.getMessage());
    }

    @Override
    public void onSuccess(Long result)
    {

      //trafficService.getCurrentSchedule("Maillingerstr.", asyncCallback);
      trafficService.getUpdate(asyncCallback);
    }

  }

  // Create our "tiny" marker icon
  private final Map<PinIconEnum, Icon> myPinIconMap = new HashMap<PinIconEnum, Icon>();

  private void buildUi()
  {
    // Open a map centered on Marienplatz, Munich
    LatLng munichCityCenter = LatLng.newInstance(48.137268, 11.575406);

    final MapWidget map = new MapWidget(munichCityCenter, 14);
    map.setSize("100%", "100%");
    // Add some controls for the zoom level
    map.addControl(new LargeMapControl());

    for (PinIconEnum type : PinIconEnum.values())
    {
      myPinIconMap.put(type, createPinIcon(type));
    }

    MarkerOptions options = MarkerOptions.newInstance();
    options.setIcon(myPinIconMap.get(PinIconEnum.BLUE));

    // Add a marker
    map.addOverlay(new Marker(munichCityCenter, options));
    //map.addOverlay(new Marker(munichCity));



    // Add an info window to highlight a point of interest
    //map.getInfoWindow().open(map.getCenter(), new InfoWindowContent("World's Largest Ball of Sisal Twine"));

 
    final DockLayoutPanel dock = new DockLayoutPanel(Unit.PX);
    dock.add(map);

    // Add the map to the HTML host page
    RootLayoutPanel.get().add(dock);
  }


  AsyncCallback<TransportationVehicle[]> asyncCallback = new MyAcyncCallback();

  private class MyAcyncCallback implements AsyncCallback<TransportationVehicle[]>
  {

    @Override
    public void onFailure(Throwable caught)
    {
      Window.alert(caught.getMessage());
    }

    @Override
    public void onSuccess(TransportationVehicle[] resultArray)
    {
      GWT.log("received " + resultArray.length + " elements from server");
      for (int i = 0; i < resultArray.length; i++)
      {
        
      }

    }

  }


  enum PinIconEnum {BLUE, GREEN, RED, YELLOW};

  // Create our "tiny" marker icon
  private Icon createPinIcon(PinIconEnum type)
  {
    String pictureUrl = null;
    switch (type)
    {
      case BLUE:
        pictureUrl = "http://labs.google.com/ridefinder/images/mm_20_blue.png"; break;
      case GREEN:
        pictureUrl = "http://labs.google.com/ridefinder/images/mm_20_green.png"; break;
      case RED:
        pictureUrl = "http://labs.google.com/ridefinder/images/mm_20_red.png"; break;
      case YELLOW:
        pictureUrl = "http://labs.google.com/ridefinder/images/mm_20_yellow.png"; break;
    }
    Icon pinIcon = Icon.newInstance(pictureUrl);
    pinIcon.setShadowURL("http://labs.google.com/ridefinder/images/mm_20_shadow.png");
    pinIcon.setIconSize(Size.newInstance(12, 20));
    pinIcon.setShadowSize(Size.newInstance(22, 20));
    pinIcon.setIconAnchor(Point.newInstance(6, 20));
    pinIcon.setInfoWindowAnchor(Point.newInstance(5, 1));

    return pinIcon;
  }


}
